package yt.kpool.view.pop;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;


import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;

import yt.kpool.R;
import yt.kpool.util.SaveImageUtils;
import yt.kpool.util.ToastUtil;

/**
 * Created by th1 on 2016/11/4.
 */
public class SavePopupWindow extends PopupWindow implements View.OnClickListener {

    private View view;
    private Activity context;

    //保存
    private TextView save_tv;

    //取消
    private TextView exit_tv;


    private Bitmap bitmap;

    public SavePopupWindow(Activity context,Bitmap bitmap) {
        super(context);
        this.context = context;
        this.bitmap=bitmap;
        view = LayoutInflater.from(context).inflate(R.layout.popupwindow_save, null);
        initPop();
        initView();
    }

    /**
     * 初始化控件
     */
    private void initView() {
        save_tv = (TextView) view.findViewById(R.id.save_tv);
        save_tv.setOnClickListener(this);
        exit_tv = (TextView) view.findViewById(R.id.exit_tv);
        exit_tv.setOnClickListener(this);
    }

    /**
     * 样式
     */
    private void initPop() {
        this.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);


        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        this.setContentView(view);

        setAnimationStyle(R.style.mypopwindow_bottom_anim_style);
        ColorDrawable dw = new ColorDrawable(Color.parseColor("#00000000"));
        //设置SelectPicPopupWindow弹出窗体的背景
        setBackgroundDrawable(dw);

        backgroundAlpha(0.5f);
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
            }
        });
        //获取popwindow焦点
        setFocusable(true);
        //设置popwindow如果点击外面区域，便关闭。
        setOutsideTouchable(true);
    }

    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        context.getWindow().setAttributes(lp);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.save_tv:
                if (bitmap != null) {

                    SaveImageUtils.saveImageToGallerys(context, bitmap);

                }else
                {
                    ToastUtil.showToast(context,"图片为空");
                }

                dismiss();
                break;
            case R.id.exit_tv:
                dismiss();
                break;
        }
    }


}
